import { Component, OnInit } from '@angular/core';
import {DictService} from "@shared/utils/dict.service";
import {ApiService} from "../../services/api.service";
import {ModalRef, ModalService} from "@shared/utils";
import {QlMessageService} from "qloud-angular/package/message/message.service";
import {FormBuilder, FormGroup} from "@angular/forms";
import {PageService} from "@core/http";
import {CursorService} from "@core/http/cursor.service";

@Component({
  selector: 'app-search-tags',
  templateUrl: './search-tags.component.html',
  styleUrls: ['./search-tags.component.css'],
  providers: [ModalRef, ModalService, PageService]
})
export class SearchTagsComponent implements OnInit {
  tagname ='';
  tagcode = '';
  catalogcode = '';
  sharerange = '';
  status = '';
  tagtype = '';
  scene = '';
  calogTag =[];
  public formGroup: FormGroup = this.fb.group({})
  constructor(public dictService: DictService,
              private modalRef: ModalRef,
              private fb: FormBuilder,
              private piService: ApiService,
              private apiService: ApiService,
              public modal: ModalService,
              public pageService: PageService,
              private message: QlMessageService,
              public cursorService: CursorService) { }

  ngOnInit() {
    this.getTagCatalog()
  }
  //获取标签目录
  public getTagCatalog(){
    this.cursorService.limit = 10000;
    this.apiService.getTagCatalog(this.cursorService).subscribe(list => {
      for(let item of list){
        if(item['pcode']){
          this.calogTag.push({paramLabel:item['catalogname'],paramValue:item['catalogcode']})
        }
      }
    })
  }
  public onSubmit() {
    this.modalRef.close(true)
  }
  transform(key:string,value:string,sql:string){
    if(sql != 'select *'){
      sql += ' and ' + key +" = " + "'*" + value + "*'"
    }
    else{
      sql += " where " + key +" = " + "'*" + value + "*'";
    }
    return sql
  }
  public saveQuery() {
    let sql='select *'
    if(this.tagname!=''){
      sql= this.transform('tagname',this.tagname ,sql)
    }
    if(this.tagcode!=''){
      sql= this.transform('tagcode',this.tagcode ,sql)
    }
    if(this.catalogcode!=''){
      sql= this.transform('catalogcode',this.catalogcode ,sql)
    }
    if(this.sharerange!=''){
      sql= this.transform('sharerange',this.sharerange ,sql)
    }
    if(this.status!=''){
      sql= this.transform('status',this.status ,sql)
    }
    if(this.tagtype!=''){
      sql= this.transform('tagtype',this.tagtype ,sql)
    }
    if(this.scene!=''){
      sql= this.transform('scene',this.scene ,sql)
    }
    this.modalRef.close(sql)
    // this.apiService.saveQuery().subscribe(data => {
    //   this.message.success(data[0].message)
    // })
  }
}
